UNAME_S := $(shell uname -s)
migrations_root := /tmp/{{cookiecutter.project_name}}-dev-migrations-root
arcadia_root := $(shell ya dump root)
params := $(wordlist 2,100,$(MAKECMDGOALS))
no_db_recipe := DB_USE_ARCADIA_RECIPE=False
source_root := Y_PYTHON_SOURCE_ROOT=$(arcadia_root)
entry_point := Y_PYTHON_ENTRY_POINT=":main"
pytest_args := -m pytest -p no:warnings -sv


# Code style
ya-style:
	ya style

check-isort:
	isort --recursive --check-only --diff {{cookiecutter.project_name}}/

check-flake8:
	flake8 {{cookiecutter.project_name}}/

check-style: check-flake8 check-isort

sort-imports:
	isort --recursive {{cookiecutter.project_name}}/

mypy:
	MYPYPATH=$(arcadia_root) mypy -p {{cookiecutter.project_name}} --show-traceback --disallow-incomplete-defs

cc: ya-style sort-imports check-style mypy


# Tests
test:
	ya make -r -tt --test-stdout

test-unit:
	ya make -r -tt --test-stdout {{cookiecutter.project_name}}/tests/unit

test-functional:
	ya make -r -tt --test-stdout {{cookiecutter.project_name}}/tests/functional

dev-unit:
	$(no_db_recipe) $(source_root) $(entry_point) ./{{cookiecutter.project_name}}/tests/unit/{{cookiecutter.project_path.replace('/', '-')}}-{{cookiecutter.project_name}}-tests-unit $(pytest_args) $(if $(params),$(params),{{cookiecutter.project_name}}/tests/unit)

dev-functional:
	$(no_db_recipe) $(source_root) $(entry_point) ./{{cookiecutter.project_name}}/tests/functional/{{cookiecutter.project_path.replace('/', '-')}}-{{cookiecutter.project_name}}-tests-functional $(pytest_args) $(if $(params),$(params),{{cookiecutter.project_name}}/tests/functional)


# Development
deps:
	ya make --checkout {{cookiecutter.project_name}} && ya make --checkout {{cookiecutter.project_name}}/tests

build_bin:
	ya make bin

build_docker: build_bin
	ya package package/package.json --docker --docker-repository {{cookiecutter.src_path.split('/', 1)[0]}}/{{cookiecutter.project_name}} --custom-version 0.0.0-$(shell date +"%s")

runserver:
	$(source_root) ./bin/{{cookiecutter.project_name}} runserver

l_runserver:
	./run_local.sh make runserver

wl_runserver:
	./run_local.sh -w make runserver

runworkers:
	$(source_root) ./bin/{{cookiecutter.project_name}} runworkers

l_runworkers:
	./run_local.sh make runworkers

wl_runworkers:
	./run_local.sh -w make runworkers

shell:
	$(source_root) ./bin/{{cookiecutter.project_name}} shell

l_shell:
	./run_local.sh make shell

tunnel:
	ssh -D 4444 dev -N


# Database
start_db:
ifeq ($(UNAME_S),Linux)
	# докер в убунте не может делать bind volume на директорию аркадии, смонтированной через arc
	rm -rf $(migrations_root) && mkdir -p $(migrations_root) && cp -r ./postgre $(migrations_root)
	{{cookiecutter.PROJECT_NAME}}_MIGRATIONS_ROOT=$(migrations_root) docker-compose up -d {{cookiecutter.project_name}}_db {{cookiecutter.project_name}}_db_migrated
else
	docker-compose up -d {{cookiecutter.project_name}}_db {{cookiecutter.project_name}}_db_migrated
endif

pgcli:
	PGPASSWORD="P@ssw0rd" pgcli -h localhost -p 5252 -U {{cookiecutter.project_name}} -d {{cookiecutter.project_name}}_db
